package com.cupdata.marathon.client;

import com.cupdata.marathon.definition.RspCd;
import com.cupdata.marathon.param.acctquery.AcctQueryRequest;
import com.cupdata.marathon.param.acctquery.AcctQueryResponse;
import com.cupdata.marathon.utils.GsonUtils;
import com.unionpay.magpie.client.ServiceRegistry;
import com.unionpay.magpie.remoting.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 动帐查询接口
 */
@Service
public class AcctQueryService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String ACCT_QUERY_SERVICE = "acct_query_service";

    public AcctQueryResponse call(AcctQueryRequest request) {
        try {
            byte[] resultBytes = ServiceRegistry.getService(ACCT_QUERY_SERVICE).call(GsonUtils.gson.toJson(request).getBytes());
            String res = new String(resultBytes);
            logger.info("Received message: {}", res);
            return GsonUtils.gson.fromJson(res, AcctQueryResponse.class);
        } catch (RemotingException e) {
            logger.error("Magpie fail: {}", e.getMessage());
            AcctQueryResponse response = new AcctQueryResponse();
            response.setRspCd(RspCd.FAIL.getCode());
            response.setRspMsg("请求超时");
            return response;
        }
    }
}
